<?php
/**
 * Created by PhpStorm.
 * User: 郑好
 * Date: 2019/5/10
 * Time: 下午1:42
 */

namespace app\common;

use app\lucky\lottery\service\LotteryService;
use think\Exception;

/**
 * Class Lottery
 *
 * @package     app\common
 * @description 彩排模块 可能与 彩票社区 分离
 */
class Lottery
{
    use InstanceTrait;

    private $lotteryDomain = "";

    protected function __construct()
    {
        //        if(config("APP_ENV") == "prod"){
        //            $this->lotteryDomain = "https://server.fu51.cn/";
        //        }else{
        //            $this->lotteryDomain = "https://kaijiang-server-dev.fu51.cn/";
        //        }
        if (env("APP_ENV") == "prod") {
            $this->lotteryDomain = "https://server.fu51.cn/";
        } elseif (env("APP_ENV") == "pre") {
            $this->lotteryDomain = "http://kaijiang-server-pre.fu51.cn/";
        } else {
            $this->lotteryDomain = "http://kaijiang-server-dev.fu51.cn/";
        }
    }

    private function doGet($route, $urlParams = [])
    {
        try {
            $headers = [
                "Origin-Secret" => md5("auf6c51803h-cmit-@DSF@^6-" . date("Y-m-d") . "-xmcmit")
            ];

            $url = $this->lotteryDomain . $route;

            $response = \app\common\Http::request($url, $headers, $urlParams, "GET", []);

            return $response->body;
        } catch (Exception $e) {
            \Log::error($e->getMessage());
        }
    }

    private function doPost($route, $params = [])
    {
        try {
            $headers = [
                "Origin-Secret" => md5("auf6c51803h-cmit-@DSF@^6-" . date("Y-m-d") . "-xmcmit")
            ];

            $url = $this->lotteryDomain . $route;

            $response = \app\common\Http::request($url, $headers, $params, "POST", []);

            return $response->body;
        } catch (Exception $e) {
            \Log::error($e->getMessage());
        }
    }

    public function getLatestQG()
    {
        $route = "lottery/get_results";

        $result = $this->doGet($route);

        if (!$result) {
            return ["code" => _MSG_NOT_FOUND, "msg" => "请求数据失败", "data" => []];
        }
        return json_decode($result, true);
    }

    public function getFullByNoQG($lotteryId, $lotteryNo)
    {
        if (!LotteryService::getInstance()->checkLotteryId($lotteryId)) {
            return ["code" => _MSG_INVALID_CLIENT_PARAM, "msg" => "暂不支持该彩种预测"];
        }
        if (!LotteryService::getInstance()->checkLotteryNo($lotteryId, $lotteryNo)) {
            return ["code" => _MSG_INVALID_CLIENT_PARAM, "msg" => "期号格式错误"];
        }

        $route = "lottery/" . $lotteryId . "/get_result";

        $result = $this->doGet($route, ["lottery_no" => $lotteryNo]);

        if (!$result) {
            return ["code" => _MSG_NOT_FOUND, "msg" => "请求数据失败", "data" => []];
        }
        return json_decode($result, true);
    }

    public function getSimpleByNoQG($lotteryId, $lotteryNo)
    {
        if (!LotteryService::getInstance()->checkLotteryId($lotteryId)) {
            return ["code" => _MSG_INVALID_CLIENT_PARAM, "msg" => "暂不支持该彩种预测"];
        }
        if (!LotteryService::getInstance()->checkLotteryNo($lotteryId, $lotteryNo)) {
            return ["code" => _MSG_INVALID_CLIENT_PARAM, "msg" => "期号格式错误"];
        }

        $route = "lottery/qgcz/get_simple_by_no";

        $result = $this->doGet($route, ["lottery_id" => $lotteryId, "lottery_no" => $lotteryNo]);

        if (!$result) {
            return ["code" => _MSG_NOT_FOUND, "msg" => "请求数据失败", "data" => []];
        }
        return json_decode($result, true);
    }

    public function getLatestSimpleByQGLotteryId($lotteryId)
    {
        if (!LotteryService::getInstance()->checkLotteryId($lotteryId)) {
            return ["code" => _MSG_INVALID_CLIENT_PARAM, "msg" => "暂不支持该彩种预测"];
        }

        $route = "lottery/qgcz/get_simple";

        $result = $this->doGet($route, ["lottery_id" => $lotteryId]);

        if (!$result) {
            return ["code" => _MSG_NOT_FOUND, "msg" => "请求数据失败", "data" => []];
        }
        return json_decode($result, true);
    }

    public function getAllByQGLotteryId($lotteryId)
    {
        if (!LotteryService::getInstance()->checkLotteryId($lotteryId)) {
            return ["code" => _MSG_INVALID_CLIENT_PARAM, "msg" => "暂不支持该彩种预测"];
        }

        $route = "lottery/qgcz/get_all_by_lottery";

        $result = $this->doGet($route, ["lottery_id" => $lotteryId]);

        if (!$result) {
            return ["code" => _MSG_NOT_FOUND, "msg" => "请求数据失败", "data" => []];
        }
        return json_decode($result, true);
    }

    /**
     * 获取指定彩种最近一期开奖时间
     * @param $lotteryId
     *
    //    双色球  每周二、四、日21:20开奖
    //
    //    大乐透  每周一 、三 、六20:30开奖
    //
    //    福彩3D 每天21：15开奖
     */
    public function getLotteryLastPrizeTime($lotteryId)
    {
        $nowTime = time();
        $prizeTime = '';
        switch ($lotteryId) {
            case 'fcsd':
                $prizeTime = strtotime(date('Y-m-d 21:15'));
                if ($nowTime >= $prizeTime) {
                    $prizeTime = $prizeTime + 86400;
                }
                break;
            case 'ssq':
                //开奖当日先判断当日时间是否满足
                $prizeTime = strtotime(date('Y-m-d 21:20:00'));
                if (in_array(date('w'), [0,2,4]) && $nowTime < $prizeTime) {
                    break;
                }
                $prizeTimeArr = [
                    strtotime("next Tuesday"),
                    strtotime("next Thursday"),
                    strtotime("next Sunday")
                ];
                sort($prizeTimeArr);
                $prizeTime = $prizeTimeArr[0] + 76800;
                break;
            case 'dlt':
                //开奖当日先判断当日时间是否满足
                $prizeTime = strtotime(date('Y-m-d 20:30'));
                if (in_array(date('w'), [1,3,6]) && $nowTime < $prizeTime) {
                    break;
                }
                $prizeTimeArr = [
                    strtotime("next Monday"),
                    strtotime("next Wednesday"),
                    strtotime("next Saturday")
                ];
                sort($prizeTimeArr);
                $prizeTime = $prizeTimeArr[0] + 73800;
                break;
        }
        return $prizeTime;
    }
}
